/// <reference path="../Browser.js" />
/// <reference path="EventsBase.js" />
/// <reference path="AttachedHandler.js" />


var ____a;





Events._GetArguments = function _GetArguments(args, startIndex)
{
	if (startIndex === undefined)
		startIndex = 0;

	var res = new Array();
	for (var i = startIndex; i < args.length; ++i)
		res.push(args[i]);
	return res;
}


Events._attachedHandlers = new Array();
Events._isIe = Browser.get_Name() == "MSIE";
Events._isFF = Browser.get_Name() == "Firefox";
Events._needFakeHashChange = window.onhashchange === undefined;
Events._needFakeMouseEnter = !Events._isIe;
Events._needFakeMouseLeave = !Events._isIe;
Events._needFixMouseWheel = Events._isFF;
Events._needToCleanup = Events._isIe;
Events._needFixingWheelDelta = Events._isIe;
Events._needToPassEvent = Events._isIe;
Events._needToDefaultBinding = Events._isIe;
Events._needFakeKeyPress = Events._isIe;

Events._EnsureAttachedEvents = function _EnsureAttachedEvents(obj)
{
	if (obj._attachedHandlers == null)
	{
		obj._attachedHandlers = new Array();

		if (Events._needToCleanup)
		{
			Events._attachedHandlers.push(obj._attachedHandlers);
		}
	}
}

Events._window_Unload = function _window_Unload()
{
	for (var iList = 0; iList < Events._attachedHandlers.length; ++iList)
	{
		var handlerList = Events._attachedHandlers[iList];

		for (var iHandler = 0; iHandler < handlerList.length; ++iHandler)
		{
			var ae = handlerList[iHandler];

			ae._Remove();
		}
	}
}

Events.Add = function Add(obj, type, handler, binding, argument_list)
{
	Events._EnsureAttachedEvents(obj);
	type = type.toLowerCase();
	argument_list = Events._GetArguments(arguments, 4);

	var ah = null;

	if (Events._needToDefaultBinding && binding == null)
		binding = obj;

	if (Events._needFakeMouseEnter && (type == "mouseenter" || type == "mouseleave"))
	{
		ae = new MouseEnterLeaveHandler(obj, type, handler, binding, argument_list);
	}
	else if (Events._needFakeKeyPress && type == "keypress")
	{
		ae = new KeyPressHandler(obj, handler, binding, argument_list);
	}
	else if (type == "mousewheel")
	{
		ae = new MouseWheelHandler(obj, handler, binding, argument_list);
	}
	else if (type == "domready")
	{
		ae = new DOMReadyHandler(obj, handler, binding, argument_list);
	}
	else if (Events._needFakeHashChange && obj == window && type == "hashchange")
	{
		ae = new HashChangeHandler(obj, handler, binding, argument_list);
	}
	else
	{
		ae = new AttachedHandler(obj, type, handler, binding, argument_list);
	}
	ae.Add();
	obj._attachedHandlers.push(ae);
}
Events.Remove = function Remove(obj, type, handler, binding)
{
	Events._EnsureAttachedEvents(obj);

	for (var i = 0; i < obj._attachedHandlers.length; ++i)
	{
		var ae = obj._attachedHandlers[i];
		if (____a) ae = new _AttachedEvent();

		if (ae._type == type && ae._binding == binding && ae._handler == handler)
		{
			ae._Remove();

			obj._attachedHandlers.splice(i, 1);
		}
	}
}
Events.GetAttachedHandlers = function GetAttachedHandlers(obj)
{
	Events._EnsureAttachedEvents(obj);
	return obj._attachedHandlers;
}

Events._AddEventObject = function AddEvent(type, handler, binding, arguments_list)
{
	var args = new Array();
	for (var i = 0; i < arguments.length; ++i)
		args.push(arguments[i]);
	Events.Add.apply(window, [this].concat(args));
}
Events._RemoveEventObject = function RemoveEvent(type, handler, binding)
{
	Events.Remove(this, type, handler, binding);
}
if (typeof (Element) != "undefined")
{
	Element.prototype.AddEvent = window.AddEvent = document.AddEvent = Events._AddEventObject;
	Element.prototype.RemoveEvent = window.RemoveEvent = document.RemoveEvent = Events._RemoveEventObject;
}

if (Events._needToCleanup)
	window.AddEvent("unload", Events._window_Unload);
